package com.jie.jie.cangtoushi;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jaydenxiao.common.commonutils.LogUtils;
import com.jie.jie.R;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CangtoushiActivity extends AppCompatActivity {

    @Bind(R.id.wuyan)
    RadioButton wuyan;
    @Bind(R.id.qiyan)
    RadioButton qiyan;
    @Bind(R.id.r1)
    RadioGroup r1;
    @Bind(R.id.cangtou)
    RadioButton cangtou;
    @Bind(R.id.cangwei)
    RadioButton cangwei;
    @Bind(R.id.cangzhong)
    RadioButton cangzhong;
    @Bind(R.id.dizeng)
    RadioButton dizeng;
    @Bind(R.id.dijian)
    RadioButton dijian;
    @Bind(R.id.r2)
    RadioGroup r2;
    @Bind(R.id.ed_shuru)
    EditText edShuru;
    @Bind(R.id.btn)
    Button btn;
    @Bind(R.id.activity_cangtoushi)
    LinearLayout activityCangtoushi;
    @Bind(R.id.shuangju)
    RadioButton shuangju;
    @Bind(R.id.shuangju1)
    RadioButton shuangju1;
    @Bind(R.id.yisan)
    RadioButton yisan;
    @Bind(R.id.r3)
    RadioGroup r3;
    @Bind(R.id.lv)
    ListView lv;
    private String num;
    private String type;
    private String yayuntype;
    private String encode;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cangtoushi);
        ButterKnife.bind(this);
        num = "5";
        type = "1";
        yayuntype = "1";
        r1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.wuyan:
                        num = "5";
                        break;
                    case R.id.qiyan:

                        num = "7";
                        break;
                }
            }
        });
        r2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.cangtou:
                        type = "1";
                        break;
                    case R.id.cangzhong:
                        type = "2";
                        break;
                    case R.id.cangwei:
                        type = "3";
                        break;
                    case R.id.dizeng:
                        type = "4";
                        break;
                    case R.id.dijian:

                        type = "5";
                        break;
                }
            }
        });

        r3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.shuangju:
                        yayuntype = "1";
                        break;
                    case R.id.shuangju1:
                        yayuntype = "2";
                        break;
                    case R.id.yisan:
                        yayuntype = "3";
                        break;
                }
            }
        });


    }

    public String streamToString(InputStream is) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            baos.close();
            is.close();
            byte[] byteArray = baos.toByteArray();
            return new String(byteArray);
        } catch (Exception e) {
            return null;
        }
    }

    @OnClick(R.id.btn)
    public void onClick() {
        if (edShuru.getText().toString() == null) {
            Toast.makeText(this, "请输入要藏头的语句", Toast.LENGTH_SHORT).show();
        } else {

            try {
                encode = URLEncoder.encode(edShuru.getText().toString().trim(), "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } finally {
            }

            final String u = "http://route.showapi.com/950-1?showapi_appid=33235&showapi_timestamp=" + System.currentTimeMillis() + "&showapi_sign=aebd3a4e44b749d69002ef1cb59c9981&num=" + num + "&type=" + type + "&yayuntype=" + yayuntype + "&key=" + encode + "";
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        URL url = new URL(u);
                        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                        conn.setReadTimeout(3000);
                        conn.setConnectTimeout(3000);
                        conn.connect();
                        if (conn.getResponseCode() == 200) {
                            String s = streamToString(conn.getInputStream());
                            final Cangtoushi cangtoushi = new Gson().fromJson(s, Cangtoushi.class);
                            LogUtils.loge(s);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    MyAdapter adapter = new MyAdapter(CangtoushiActivity.this, cangtoushi.getShowapi_res_body().getList());
                                    lv.setAdapter(adapter);
                                }
                            });
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                    }
                }
            }).start();
        }
    }
}
